package cn.hsw.manage.user.service.impl;

import cn.hsw.component.core.util.BCrypt;
import cn.hsw.component.core.util.IdUtil;
import cn.hsw.component.core.util.Validator;
import cn.hsw.manage.center.service.UserRoleCenterService;
import cn.hsw.manage.role.service.RoleService;
import cn.hsw.manage.user.entity.User;
import cn.hsw.manage.user.mapper.UserMapper;
import cn.hsw.manage.user.service.UserService;
import cn.hsw.manage.user.vo.UserDetailVo;
import cn.hsw.manage.user.vo.UserListVo;
import cn.hsw.manage.user.vo.UserVO;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李子木
 * @since 2021-11-03 08:55:22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    RoleService roleService;
    @Autowired
    UserRoleCenterService userRoleCenterService;

    @Override
    public User selectUserByPhone(String phone) {
        return baseMapper.selectUserByPhone(phone);
    }

    @Override
    public void add(UserVO userVO) {

        User user = BeanUtil.copyProperties(userVO, User.class);
        /*生成密码*/
        String password = BCrypt.hashpw(user.getPassword(), BCrypt.gensalt());
        user.setPassword(password);
        user.setSalt(BCrypt.hashpw(IdUtil.generate().toString(), BCrypt.gensalt()));
        baseMapper.insert(user);

        //后台管理新增用户绑定角色
        userRoleCenterService.bind(user.getPid(),userVO.getRolePid());
    }

    @Override
    public void edit(Long pid, String realName, String remark, String rolePid) {
        User user = baseMapper.selectById(pid);
        if(Validator.isNotEmpty(realName)){
            user.setRealName(realName);
        }
        if(Validator.isNotEmpty(remark)){
            user.setRemark(remark);
        }
        baseMapper.updateById(user);
        if(Validator.isNotEmpty(rolePid)){
            userRoleCenterService.bind(pid,rolePid);
        }

    }

    @Override
    public IPage<UserListVo> findByUserNameAndPhoneLike(String userName, String phone, Page page) {
        return baseMapper.findByUserNameAndPhoneLike(userName,phone,page);
    }

    @Override
    public String resetPassword(Long pid) {
        final User userModel = baseMapper.selectById(pid);

        String newPassword = RandomUtil.randomString("abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+-=1234567890",10);
        /*生成密码*/
        String password = BCrypt.hashpw(newPassword, BCrypt.gensalt());

        userModel.setPassword(password);
        userModel.setSalt(BCrypt.hashpw(IdUtil.generate().toString(), BCrypt.gensalt()));
        baseMapper.updateById(userModel);

        return newPassword;
    }

    @Override
    public void edit(User user) {
        this.updateById(user);
    }

    @Override
    public User findByUserName(String userName) {
        return baseMapper.findByUserName(userName);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByPid(Long pid) {
        userRoleCenterService.deleteByUserPid(pid);
        baseMapper.deleteById(pid);
    }

    @Override
    public UserDetailVo findByPid(Long pid) {
        return baseMapper.findByPid(pid);
    }
}
